#!/usr/bin/env python
# Author:XXX

import json

# Constant
filename="data.txt"
out_filename="output.txt"
subsystem_pattern=['（一）','（二）','（三）','（四）','（五）','（六）','（七）']
data_item={}
subsystem_name=""
module_list=[]
keywords_list=[]
#subkeywords_list=[]


with open(filename, 'r') as f:
    lines = f.readlines()
    for idx in range(len(lines)):
        ## Main system title ##
        if idx == 0:
            title = lines[0]
            print(title)
            keywords_list.append(title.replace("系统","").replace("\n",""))
            data_item["main"] = keywords_list[0]
        else:
            line = lines[idx].split("\t")[0]
            if idx == len(lines):
                data_item[subsystem_name] = module_list

            ## Subsystem title ##
            if line[0:3] in subsystem_pattern:
                if subsystem_name != "":
                    data_item[subsystem_name] = module_list
                keywords_list.append(line.replace(keywords_list[0],"").replace("子系统","").replace("“","").replace("”","").replace("\n","")[3:])
                subsystem_name = keywords_list[-1]
                module_list=[]
            else:
                ## Module item ##
                module_list.append(line.replace(keywords_list[0],"").replace(keywords_list[-1],""))
                if idx < 15:
                    print(json.dumps(module_list, ensure_ascii=False))

data_item[keywords_list[0]] = keywords_list[1:]
with open("output.txt","w") as fp:
    fp.write(json.dumps(data_item, ensure_ascii=False, sort_keys=True, indent=1))
print(keywords_list[0])
jstr = json.dumps(keywords_list, ensure_ascii=False)
print(jstr)
